package com.shs.riggy.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shs.riggy.common.BaseContext;
import com.shs.riggy.common.R;
import com.shs.riggy.entity.ShoppingCart;
import com.shs.riggy.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author 晚点再动心
 * @Date 2022/9/21
 */
@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping("/add")
    //添加购物车
    public R<ShoppingCart> addCart(@RequestBody ShoppingCart shoppingCart){
        log.info("添加购物车数据为：" ,shoppingCart);
        //1.设置用户id，指定当前是哪一个用户添加的购物车数据
        Long userId = BaseContext.getId();
        shoppingCart.setUserId(userId);

        //2.查询当前菜品或者套餐在购物车中是否存在
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userId);
        Long dishId = shoppingCart.getDishId();
        if (dishId != null){
            //添加到购物车的是菜品
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else {
            //添加到购物车的是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        //查询当前菜品或者套餐是否在购物车中
        ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapper);
        //2.1 如果数据已经存在，就在原有数据的基础上加一
        if (cartServiceOne != null){
            //获取原先的熟练
            Integer number = cartServiceOne.getNumber();
            cartServiceOne.setNumber(number + 1 );
            shoppingCartService.updateById(cartServiceOne);
        }else {
            //2.2 如果不存在，则添加到购物车中，此时数量默认为1
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            cartServiceOne = shoppingCart;

        }

        return R.success(cartServiceOne);
    }

    @PostMapping("/sub")
    //添加购物车
    public R<String> subCart(@RequestBody ShoppingCart shoppingCart){
        log.info("购物车数据为：" ,shoppingCart);
        //1.设置用户id，指定当前是哪一个用户添加的购物车数据
        Long userId = BaseContext.getId();
        shoppingCart.setUserId(userId);

        //2.查询当前菜品或者套餐在购物车中是否存在
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userId);
        Long dishId = shoppingCart.getDishId();
        if (dishId != null){
            //添加到购物车的是菜品
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else {
            //添加到购物车的是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        //查询当前菜品或者套餐是否在购物车中
        ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapper);
        //2.1 如果数据已经存在，就在原有数据的基础上加一
        if (cartServiceOne != null){

            //获取原先的熟练
            Integer number = cartServiceOne.getNumber();
            if (number != 0){
                cartServiceOne.setNumber(number - 1);
                shoppingCartService.updateById(cartServiceOne);
            }else {
                shoppingCartService.remove(queryWrapper);
            }

        }

        return R.success("购物车订单删减成功！");
    }

    //查看购物车数据
    @GetMapping("/list")

    public R<List<ShoppingCart>> list(){
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        //根据用户id查询用户购物车数据
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getId());
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);

        return R.success(list);
    }

    //清空购物车
    @DeleteMapping("/clean")
    public R<String> cleanCart(){

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        //根据用户ID清除购物车数据
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getId());
        shoppingCartService.remove(queryWrapper);
        return R.success("清空购物车成功！");
    }

}
